package com.example.administrator.x3picturetransliteration.Home.Activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.x3picturetransliteration.Base.BaseActivity;
import com.example.administrator.x3picturetransliteration.Base.BasePresenter;
import com.example.administrator.x3picturetransliteration.Home.Presenter.Activity.AdvertisementPresenter;
import com.example.administrator.x3picturetransliteration.R;
import com.example.administrator.x3picturetransliteration.Util.Img.PicassoEqualProportionTransformation2;
import com.squareup.picasso.Picasso;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 广告页
 * Created by Administrator on 2019/8/16.
 */

public class AdvertisementActivity extends BaseActivity{

    public static void StartAdvertisementActivity(Context context){

        Intent intent = new Intent(context,AdvertisementActivity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.img)ImageView img; //广告图片
    @BindView(R.id.TimeText)TextView TimeText; //时间;

    private AdvertisementPresenter mAdvertisementPresenter;

    private boolean judeg = false;
    private volatile int Time = 3;

    private String url = null;

    @Override
    public int intiLayout() {
        return R.layout.home_activity_advertisement;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mAdvertisementPresenter = new AdvertisementPresenter(this,mCompositeSubscriptions);

        mAdvertisementPresenter.ad(new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {

                if (data==null){

                    Close();

                }else {

                    Map<String,String> map = (Map<String,String>)data;

                    Picasso.with(AdvertisementActivity.this)
                            .load(map.get("img"))
                            .transform(new PicassoEqualProportionTransformation2(img))
                            .into(img);

                    url = map.get("url");

                    go();

                }

            }
        });



    }

    @OnClick(R.id.SkipView)
    public void SkipViewClick(){

        Close();
    }

    @OnClick(R.id.img)
    public void imgClick(){

        if (url==null)return;

        downloadNewApk(url);
        judeg = true;
        finish();
    }

    private synchronized void Close(){

        judeg = true;
        startActivity(new Intent(this,SignInActivity.class));
        finish();

    }

    private void downloadNewApk(String path) {

        mAdvertisementPresenter.Statistics("1");

        Uri uri = Uri.parse(path);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(uri);
        this.startActivity(intent);


    }

    private void go(){

        new Thread(new Runnable() {
            @Override
            public void run() {

                while (!judeg){

                    AdvertisementActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            TimeText.setText(Time + "S");
                        }
                    });

                    if (Time==0){

                        Close();
                        break;
                    }

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    Time = Time - 1;

                }

            }
        }).start();

    }
}
